home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_8 / issue_07 / std / !STDFinder / c / validate < prev    next >
Encoding:
Text File  |  1993-03-08  |  1.6 KB  |  59 lines

  1. /* Purpose - validate the search string */
  2.  
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. #include "string.h"
  6. #include "ctype.h"
  7. #include "werr.h"
  8.  
  9. extern int s_type,leading,trailing,wildcards;
  10.  
  11. int validate(char *string,int exact)
  12. {
  13.   char *pointer,temp[80];
  14.   char *string_to_lower(char *string);
  15.  
  16.   leading = trailing = wildcards = 0;
  17.  
  18.   strcpy(temp,string_to_lower(string));
  19.   pointer = temp;
  20.   if (*pointer != '*') {
  21.     if (isdigit(*pointer)) s_type = 0; /* then is a code */
  22.     else if (isalpha(*pointer) || ispunct(*pointer)) s_type = 1; /* then is a town */
  23.     else {
  24.       werr(0,"Syntax error in search string. Please re-enter search string.");
  25.       return 0;
  26.       }
  27.     }
  28.   else {
  29.     leading = 1;
  30.     pointer++;
  31.     if (*pointer == '*') {
  32.     werr(0,"Syntax error in search string. Only one leading wildcard allowed.");
  33.     return 0;
  34.     }
  35.     if (isdigit(*pointer)) s_type = 0; /* then is a code */
  36.     else if (isalpha(*pointer) || ispunct(*pointer)) s_type = 1; /* then is a town */
  37.     else {
  38.       werr(0,"Syntax error in search string. Please re-enter search string.");
  39.       return 0;
  40.       }
  41.    }
  42.  
  43.   pointer = temp + strlen(temp) - 1; /* point to last character */
  44.   if (*pointer == '*') trailing = 1;
  45.  
  46. /* finally check that there are not more than 2 wildcards */
  47.   pointer = temp;
  48.   while(*pointer) if(*pointer++ == '*') wildcards++;
  49.   if (wildcards > 2) { 
  50.     werr(0,"Syntax error in search string. Too many wildcards present."); 
  51.     return 0;
  52.   }
  53.   if (exact && wildcards) {
  54.     werr(0,"Error - cannot have an exact search with wildcards. Please try again");
  55.     return 0;
  56.   }
  57.   return 1;  /* if here then all is OK */
  58. }
  59.